package com.cheyun.netsalev3.viewmodel

import android.util.Log
import android.view.View
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.*
import com.cheyun.netsalev3.R
import com.cheyun.netsalev3.bean.CustomListParam
import com.cheyun.netsalev3.widget.BaseDialog


/**
 * @author Michael
 * @date  2018/11/8 - 11:12.
 */
class CustomListActivityViewModel : ViewModel(), BaseDialog.BaseDialogListener {

    //    NSArray *array = @[@"客户姓名:uname",@"联系电话:phone",@"拟购车系:series",@"建档时间:dateline"];
    private var names = arrayListOf(
//            CustomListParam("建档时间", 0, R.drawable.b01_6_zdyxsx_tos, "dateline"),
            CustomListParam("业务类型", 0, R.drawable.b01_6_zdyxsx_tos, "infotype"),
            CustomListParam("信息渠道", 1, R.drawable.b01_6_zdyxsx_infobahn, "infoplace"),
            CustomListParam("信息来源", 2, R.drawable.b01_6_zdyxsx_source, "infosource")

    )
    //弹窗数据
    var Poplists = MutableLiveData<List<CustomListParam>>()
    //列表数据
    var lists = MediatorLiveData<List<CustomListParam>>()

    init {
        Poplists.postValue(names)
        var temlist = Transformations.switchMap(Poplists) {
            val result = MutableLiveData<List<CustomListParam>>()
            var tem = ArrayList<CustomListParam>()
            for (item in it) {
                if (item.IsSelect) {
                    tem.add(item)
                }
            }
            Log.e("switchMap", "............")
            result.postValue(tem)
            result
        }
        lists.addSource(temlist, lists::setValue)
    }

    fun setdata(lis: ArrayList<CustomListParam>) {
        Log.e("setdata", lis.toString())
        for (item in names) {
            for (tem in lis) {
                if (item.id == tem.id) {
                    item.IsSelect = true
                }
            }
        }
        Log.e("initData", names.toString())
        Poplists.postValue(names)
    }


    fun onClickAdd(view: View) {
        var content = view.context
        if (content is AppCompatActivity) {
            val newFragment = BaseDialog()
            newFragment.listener = this
            newFragment.show(content.supportFragmentManager, "missiles")
        }
    }

    fun onRemoveItem(view: View, item: CustomListParam) {
        item.IsSelect = false
        Poplists.postValue(names)
    }


    /**弹窗相关**/


    override var title: String = "自定义选项"

    override var content: String? = null
    override var layoutId: Int = R.layout.base_dialog_list_item

    override var list: MutableLiveData<List<*>>
        get() {
            //筛选数据  根据列表数据把弹窗数据（Poplists）对应位置设置为选中
            var listtem = lists.value
            for (i in 0 until Poplists.value?.size!!) {
                Poplists.value?.get(i)?.IsSelect = false
                for (j in 0 until listtem?.size!!) {
                    if (Poplists.value?.get(i)?.id == listtem[j].id) {
                        Poplists.value?.get(i)?.IsSelect = true
                    }
                }
            }
            return Poplists as MutableLiveData<List<*>>
        }
        set(value) {
            list = value
        }

    override fun onClickCancel(view: View) {
    }

    override fun onClickOk(view: View) {
        Poplists.postValue(names)
    }

    override fun onClickItem(view: View, param: Any) {
        var i = 0
        for (item in names) {
            if (item.IsSelect) {
                i++
            }
        }
        if (param is CustomListParam) {
            if (i > 4 && !param.IsSelect) {
                Toast.makeText(view.context, "抱歉最多显示5项", Toast.LENGTH_LONG).show()
                return
            }
            param.IsSelect = !param.IsSelect
        }
    }
    /**弹窗相关**/

}